﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security;
using System.Security.Cryptography;
using MUD.Lib.Utils;
using MUD.Lib.Objects.Users;

namespace MUD.Lib.Utils
{
    internal class InputTrap<T>
    {
        protected delegate bool InputHandler(User user, string input);

        protected Dictionary<T, InputHandler> StateMachine;

        protected T _state;

        protected User.ReceiveHandler Listener;

        public InputTrap(User user)
        {
            Listener = new User.ReceiveHandler(ReceiveInput);
            user.OnExclusiveReceive += Listener;
            StateMachine = new Dictionary<T, InputTrap<T>.InputHandler>();
        }

        public bool ReceiveInput(User user, string input)
        {
            return StateMachine[_state](user, input.TrimNewline());
        }

        protected void AddStage(T state, InputHandler method)
        {
            if (!StateMachine.Keys.Contains<T>(state))
                StateMachine.Add(state, method);
        }
    }
}
